package com.siruier.boss.ui.activity.mall

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.core.view.isVisible
import com.kingja.loadsir.core.LoadService
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.mallApi
import com.siruier.boss.bean.GoodsOrderStateEnum
import com.siruier.boss.bean.OrderDetailBean
import com.siruier.boss.common.emptyGone
import com.siruier.boss.databinding.ActivityUserOrderDetailsBinding
import com.siruier.boss.ui.adapter.ConfirmOrderShopAdapter
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.ext.*
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.helper.loadsir.LoadSirHelper
import com.siruier.boss.ui.utils.FullyLinearLayoutManager
import com.siruier.boss.ui.viewmodel.OrderViewModel
import com.siruier.boss.utils.ViewUtils

class UserOrderDetailsActivity : BaseActivity<ActivityUserOrderDetailsBinding>() {
    private val mOrderViewModel: OrderViewModel by viewModels()
    private val mOrderSn: String by InjectBundle()
    private val type: Int by InjectBundle { 1 }
    private var mOrderDetailBean: OrderDetailBean? = null
    private val mLoadService: LoadService<*> by lazy { LoadSirHelper.register(vb.rootView) { onReload() } }
    private val mConfirmOrderShopAdapter = ConfirmOrderShopAdapter()

    override fun initView() {
        bindViewClick(
            vb.buttonCopy,
            vb.buttonCopyShipSn,
            vb.textOrderButtonPay,
            vb.textOrderButtonCancel,
            vb.textOrderButtonLogistics,
            vb.textOrderButtonConfirmReceip,
            vb.textOrderButtonEvaluate,
            vb.textOrderButtonDelete
        )
        vb.rvOrder.adapter = mConfirmOrderShopAdapter
        vb.rvOrder.layoutManager = FullyLinearLayoutManager(this)
        mOrderViewModel.handSuccess.observe(this) {
            setResult(RESULT_OK)
            onReload()
        }
    }

    override fun onClick(v: View?) {
        mOrderDetailBean?.apply {
            when (v) {
                vb.buttonCopy -> orderSn.clipboard()
                vb.buttonCopyShipSn -> shipSn.clipboard()
                vb.textOrderButtonPay -> mOrderViewModel.handOrderClick(mThis, this, v.id)
                vb.textOrderButtonCancel -> mOrderViewModel.handOrderClick(mThis, this, v.id)
                vb.textOrderButtonLogistics -> startActivityExt<LogisticsActivity> {
                    putExtra("orderSn", orderSn)
                }
            }

        }
    }

    override fun initData() {
        mLoadService.showLoadingCallback()
        onReload()
    }

    private fun onReload() {
        launchUI({
            if (type == 2) {
                mallApi.giveOrderDetail(mOrderSn).toData()
            } else {
                mallApi.orderDetail(mOrderSn).toData()
            }
        }, ApiObserver(loadService = mLoadService) {
            mOrderDetailBean = it
            it?.apply {
                vb.tvCountDown.gone()
                mOrderViewModel.cancelOrderCountDownJob()
                if (GoodsOrderStateEnum.OBLIGATION.state == orderStatus) {// 处理待付款倒计时
                    vb.tvCountDown.isVisible = ramainSeconds > 0
                    mOrderViewModel.orderCountDown(ramainSeconds) {
                        if (1 == it) onReload()// 订单超时，刷新页面
                        vb.tvCountDown.text = getString(R.string.text_user_order_details_surplus, it / 60, it % 60)
                    }
                }
                mConfirmOrderShopAdapter.datas = mutableListOf(this)
                mConfirmOrderShopAdapter.notifyDataSetChanged()
                vb.tvStatus.text = UserOrderCenterActivity.mOrderShowStateMap[orderStatus]
                vb.groupAddress.isVisible = address.isNotEmpty()
                vb.tvName.text = consignee
                vb.tvPhome.text = mobile
                vb.tvAddressDetails.text = address
                vb.tvOrderNo.emptyGone(orderSn, R.string.text_user_order_details_no)
                vb.tvCreateTime.emptyGone(createTime, R.string.text_user_order_details_create_time)
                vb.tvSendTime.emptyGone(shipTime, R.string.text_user_order_details_send_time)
                vb.tvPayTime.emptyGone(payTime, R.string.text_user_order_details_pay_time)
                vb.tvDealTime.emptyGone(confirmTime, R.string.text_user_order_details_deal_time)
                vb.tvShipChannel.emptyGone(shipChannel, R.string.text_user_order_details_ship_channel)
                vb.tvShipSn.emptyGone(shipSn, R.string.text_user_order_details_ship_sn)
                vb.buttonCopyShipSn.isVisible = shipSn.isNotBlank()

                vb.tvOrderNo.textSpan(vb.tvOrderNo.text.toString()) {
                    spanText = orderSn
                    addForegroundColorSpan(R.color.text_hint_color_2.toColor())
                }
                vb.tvCreateTime.textSpan(vb.tvCreateTime.text.toString()) {
                    spanText = createTime
                    addForegroundColorSpan(R.color.text_hint_color_2.toColor())
                }
                vb.tvSendTime.textSpan(vb.tvSendTime.text.toString()) {
                    spanText = shipTime
                    addForegroundColorSpan(R.color.text_hint_color_2.toColor())
                }
                vb.tvPayTime.textSpan(vb.tvPayTime.text.toString()) {
                    spanText = payTime
                    addForegroundColorSpan(R.color.text_hint_color_2.toColor())
                }
                vb.tvDealTime.textSpan(vb.tvDealTime.text.toString()) {
                    spanText = confirmTime
                    addForegroundColorSpan(R.color.text_hint_color_2.toColor())
                }
                vb.tvShipChannel.textSpan(vb.tvShipChannel.text.toString()) {
                    spanText = shipChannel
                    addForegroundColorSpan(R.color.text_hint_color_2.toColor())
                }
                vb.tvShipSn.textSpan(vb.tvShipSn.text.toString()) {
                    spanText = shipSn
                    addForegroundColorSpan(R.color.text_hint_color_2.toColor())
                }
                ViewUtils.setVisibility(
                    View.GONE,
                    vb.textOrderButtonPay,
                    vb.textOrderButtonCancel,
                    vb.textOrderButtonLogistics,
                    vb.textOrderButtonConfirmReceip,
                    vb.textOrderButtonEvaluate,
                    vb.textOrderButtonDelete
                )
                vb.llBottom.visible()
                when (orderStatus) {
                    GoodsOrderStateEnum.OBLIGATION.state ->
                        ViewUtils.setVisibility(
                            View.VISIBLE,
                            vb.textOrderButtonPay,
                            vb.textOrderButtonCancel
                        )
                }
                if (shipSn.isNotEmpty()) {
                    ViewUtils.setVisibility(View.VISIBLE, vb.textOrderButtonLogistics)
                }
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        onReload()
    }

}